﻿using Com.Lancetinc.Evs.Helpers;
using System.Windows;
using System.Windows.Input;

namespace Com.Lancetinc.Evs.Windows
{
    /// <summary>
    /// LicenseWindow.xaml 的交互逻辑
    /// </summary>
    public partial class LoginWindow
    {
        public LoginWindow()
        {
            InitializeComponent();
            Title = AppConsts.AppTitle;
            CtrlPassword.Focus();
            this.Loaded += LoginWindow_Loaded;
        }

        private void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            SplashScreenWin.CloseWin();
        }

        private void BtnExit_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void BtnLogin_Click(object sender, RoutedEventArgs e)
        {
            var password = CtrlPassword.Password.Trim();
            if (password.Length == 0)
            {
                MessageHelper.ShowWarning(I18nHelper.Default.GetMessage("LoginWindow.PleaseInputPassword"));
                return;
            }

            if (!UserHelper.CheckPassword(password))
            {
                MessageHelper.ShowWarning(I18nHelper.Default.GetMessage("LoginWindow.PasswordWrong"));
                return;
            }

            // 自动登录
            bool autoLogin = ChkAutoLogin.IsChecked == true;
            string pwd = autoLogin ? password : null;
            LoginHelper.SetAutoLogin(autoLogin, pwd);

            DialogResult = true;
        }

        private void CtrlPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                BtnLogin_Click(sender, null);
            }
        }
    }
}